import { kr36 } from "@/servers/crawler";
import { PAGE_SIZE } from "../Common/commonData";
import { TSKrNewsFlashesCard } from "./TSKrNewsFlashesCard";
import { ThirtySixKrCard } from "./ThirtySixKrCard";

const items = [
  {
    key: "newsFlashes",
    label: "快讯",
  },
  {
    key: "webNews",
    label: "最新资讯",
  },
  {
    key: "hotRenQi",
    label: "人气榜",
  },
  {
    key: "hotZongHe",
    label: "综合榜",
  },
  {
    key: "hotShouCang",
    label: "收藏榜",
  },
];

const TABS = Object.freeze({
  newsFlashes: "newsFlashes",
  webNews: "webNews",
  hotRenQi: "hotRenQi",
  hotZongHe: "hotZongHe",
  hotShouCang: "hotShouCang",
});

/**
 * 根据tab, page返回初始分页数据
 * @param {String} tab
 * @param {number} page
 * @param {number} 分页大小
 * @returns {Object} 分页数据
 */
const getHotDataByTab = async (page, tab, currentPageSize = PAGE_SIZE) => {
  switch (tab) {
    case TABS.newsFlashes:
      return await kr36.getFlashPage(page, currentPageSize);
    case TABS.hotRenQi:
      return await kr36.getHotPage(page, currentPageSize);
    case TABS.hotShouCang:
      return await kr36.getBookmarkPage(page, currentPageSize);
    case TABS.hotZongHe:
      return await kr36.getZonghePage(page, currentPageSize);
    case TABS.webNews:
      return await kr36.getLatestPage(page, currentPageSize);
  }
};

const initErrorMessage = {
  [TABS.hotRenQi]: "人气榜信息获取失败",
  [TABS.hotShouCang]: "收藏榜信息获取失败",
  [TABS.hotZongHe]: "综合榜信息获取失败",
  [TABS.newsFlashes]: "快讯信息获取失败",
  [TABS.webNews]: "最新资讯信息获取失败",
};

const loaderErrorMessage = {
  [TABS.hotRenQi]: "人气榜信息加载失败",
  [TABS.hotShouCang]: "收藏榜信息加载失败",
  [TABS.hotZongHe]: "综合榜信息加载失败",
  [TABS.newsFlashes]: "快讯信息加载失败",
  [TABS.webNews]: "最新资讯信息加载失败",
};

const TOP_THREE = {
  1: "https://static.36krcdn.com/36kr-web/static/ic_no1@3x.31fb23f9.png",
  2: "https://static.36krcdn.com/36kr-web/static/ic_no2@3x.d62cf7be.png",
  3: "https://static.36krcdn.com/36kr-web/static/ic_no3@3x.a32658e7.png",
};

const getCardListByTab = (tab, hotRankings) => {
  const now = new Date();
  const midnight = new Date(now.getFullYear(), now.getMonth(), now.getDate());
  const beforeMidnight = new Date(
    now.getFullYear(),
    now.getMonth(),
    now.getDate() - 1
  );

  const renderDateHeader = (text, zIndex) => (
    <div
      key={`${text}Header`}
      style={{
        background: "#fff7e8",
        position: "sticky",
        top: 0,
        zIndex,
        padding: "10px",
      }}>
      {text}
    </div>
  );

  if (tab !== TABS.newsFlashes) {
    return hotRankings.map((item, i) => (
      <ThirtySixKrCard key={`${tab}-${i}`} item={item} tab={tab} />
    ));
  }

  const todayCards = [renderDateHeader("今天", 10)];
  const yesterdayCards = [];

  hotRankings.forEach((item, i) => {
    const card = <TSKrNewsFlashesCard key={`${tab}-${i}`} item={item} />;
    if (new Date(item?.extra_content?.created_at) >= midnight) {
      todayCards.push(card);
    } else if (new Date(item?.extra_content?.created_at) >= beforeMidnight) {
      yesterdayCards.push(card);
    }
  });

  if (yesterdayCards.length > 0) {
    yesterdayCards.unshift(renderDateHeader("昨天", 11));
  }
  if (todayCards.length === 1) todayCards.pop();

  return [...todayCards, ...yesterdayCards];
};

export {
  items,
  TABS,
  initErrorMessage,
  loaderErrorMessage,
  TOP_THREE,
  getHotDataByTab,
  getCardListByTab,
};
